package com.qyj.spring_boot_02.service;

import com.alibaba.fastjson.JSONObject;
import com.qyj.spring_boot_02.bean.user.LoginVo;
import com.qyj.spring_boot_02.bean.user.User;
import com.qyj.spring_boot_02.mapper.mysql.UserMapper;
import com.qyj.spring_boot_02.util.CodeUtil;
import com.qyj.spring_boot_02.util.EmailUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    RedisTemplate<String, Object> redisTemplate;
    @Value("${qq.mail.username}")
    public String MAIL_USERNAME;
    @Value("${qq.mail.password}")
    public String PASSWORD;

    public User getUser(String name, String password) {
        return userMapper.getUserOne(name, password);
    }

    public JSONObject saveUser(String code, String name, String password, String mail) {

        User user = userMapper.getUserOneByName(name);
        if (user != null) {
            JSONObject json = new JSONObject();
            // todo
            json.put("success", -1);
            json.put("msg", "注册失败,该用户名已经存在！");
            json.put("code", "500");
            return json;
        }
        User user1 = userMapper.getUserOneByMail(mail);
        if (user1 != null) {
            JSONObject json = new JSONObject();
            json.put("success", -1);
            json.put("msg", "注册失败,该qq邮箱已经注册！\n请登录！！");
            json.put("code", "500");
            return json;
        }
        Object atCode = redisTemplate.opsForValue().get("user:register:" + name + ":code");
        if (atCode == null) {
            JSONObject json = new JSONObject();
            json.put("success", -1);
            json.put("msg", "注册失败,验证码过期了! \n 请重新获取");
            json.put("code", "500");
            return json;
        }
        String endCode = (String) atCode;
        if (endCode.equals(code)) {
            JSONObject json = new JSONObject();
            json.put("success", 1);
            json.put("msg", "注册成功");
            json.put("code", "200");
            boolean b = userMapper.saveUser(name, password, mail);
            if (!b) {
                json.put("success", -1);
                json.put("msg", "注册失败数据库 ！！");
                json.put("code", "500");
                return json;
            }
            return json;
        } else {
            JSONObject json = new JSONObject();
            json.put("success", -1);
            json.put("msg", "注册失败,验证码不对！！");
            json.put("code", "500");
            return json;
        }

    }

    public boolean getCode(String name, String mail) {
        String code = CodeUtil.getCode(6);
        redisTemplate.opsForValue().set("user:register:" + name + ":code", code);
        redisTemplate.expire("user:register:" + name + ":code", 10, TimeUnit.MINUTES);
        log.info("用户名：" + name + " 验证码：" + code);
        return EmailUtil.sessionEmail(mail, code, name, MAIL_USERNAME, PASSWORD);
    }

    public User getqqUser(String name, String password) {
        return userMapper.getqqUserOne(name, password);
    }
//121.36.202.139:8899
    public LoginVo getPwd(LoginVo loginVo) {
        if (loginVo == null) {
            LoginVo loginVo1 = new LoginVo();
            loginVo1.setStatusCode(-1);
            return loginVo1;
        }
        String name = loginVo.getName();
        String mail = loginVo.getMail();
        String code = loginVo.getCode();
        String password = loginVo.getPassword();
        if (name != null && (mail == null||mail.equals(""))) {
            User user = userMapper.getUserOneByName(name);
            if (user == null) {
                LoginVo loginVo1 = new LoginVo();
                loginVo1.setStatusCode(-1);
                loginVo1.setMsg("该账号不存在！！");
                return loginVo1;
            }
            LoginVo loginVo1 = new LoginVo();
            loginVo1.setStatusCode(-1);
            loginVo1.setMsg("请输入该账号的邮箱：" + user.getMail());
            return loginVo1;
        }
        if (name != null && !name.equals("") && mail != null && (code == null || code.equals(""))) {
            String code1 = CodeUtil.getCode(6);
            log.info("改密码的邮箱：" + mail + " 验证码：" + code1);
            redisTemplate.opsForValue().set("user:getPwd:" + name + ":code", code1);
            redisTemplate.expire("user:getPwd:" + name + ":code", 7, TimeUnit.MINUTES);
            boolean pwdEmail = EmailUtil.getPwdEmail(mail, code1, name, MAIL_USERNAME, PASSWORD);
            if (pwdEmail) {
                LoginVo loginVo1 = new LoginVo();
                loginVo1.setStatusCode(1);
                loginVo1.setMsg("验证码已经发送该账号的邮箱：" + mail);
                return loginVo1;
            } else {
                LoginVo loginVo1 = new LoginVo();
                loginVo1.setStatusCode(-1);
                loginVo1.setMsg("验证码发送失败!!");
                return loginVo1;
            }

        }
        if ((name == null || name.equals(""))&& mail != null && (code == null || code.equals("")) ) {
            String code1 = CodeUtil.getCode(6);
            log.info("改密码的邮箱：" + mail + " 验证码：" + code1);
            redisTemplate.opsForValue().set("user:getPwd:" + mail + ":code", code1);
            redisTemplate.expire("user:getPwd:" + mail + ":code", 7, TimeUnit.MINUTES);
            boolean pwdEmail = EmailUtil.getPwdEmail(mail, code1, mail, MAIL_USERNAME, PASSWORD);
            if (pwdEmail) {
                LoginVo loginVo1 = new LoginVo();
                loginVo1.setStatusCode(1);
                loginVo1.setMsg("验证码已经发送该账号的邮箱：" + mail);
                return loginVo1;
            } else {
                LoginVo loginVo1 = new LoginVo();
                loginVo1.setStatusCode(-1);
                loginVo1.setMsg("验证码发送失败!!");
                return loginVo1;
            }
        }
        if (password == null || password.equals("")) {
            LoginVo loginVo1 = new LoginVo();
            loginVo1.setStatusCode(-1);
            loginVo1.setMsg("请输入新的密码!!");
            return loginVo1;
        }
        if (name != null && !name.equals("") && mail != null && code != null) {
            Object o = redisTemplate.opsForValue().get("user:getPwd:" + name + ":code");
            if (o == null) {
                LoginVo loginVo1 = new LoginVo();
                loginVo1.setStatusCode(-1);
                loginVo1.setMsg("验证码已经过期! 请重新获取");
                return loginVo1;
            } else {
                if (code.equals(o.toString())) {
                    boolean res = userMapper.updatePwdByNameAndMail(name, mail, password);
                    if (res) {
                        LoginVo loginVo1 = new LoginVo();
                        loginVo1.setStatusCode(1);
                        loginVo1.setMsg("密码已经改正，请登录！");
                        return loginVo1;
                    } else {
                        LoginVo loginVo1 = new LoginVo();
                        loginVo1.setStatusCode(-1);
                        loginVo1.setMsg("密码改正失败，请检查账号和邮箱，或者注册！");
                        return loginVo1;
                    }
                }else {
                    LoginVo loginVo1 = new LoginVo();
                    loginVo1.setStatusCode(-1);
                    loginVo1.setMsg("密码改正失败，验证码错误！！");
                    return loginVo1;
                }
            }
        }
        if ((name == null || name.equals("")) && mail != null && code != null) {
            Object o = redisTemplate.opsForValue().get("user:getPwd:" + mail + ":code");
            if (o == null) {
                LoginVo loginVo1 = new LoginVo();
                loginVo1.setStatusCode(-1);
                loginVo1.setMsg("验证码已经过期! 请重新获取");
                return loginVo1;
            } else {
                if (code.equals(o.toString())) {
                    boolean res = userMapper.updatePwdByMail(mail, password);
                    if (res){
                        LoginVo loginVo1 = new LoginVo();
                        loginVo1.setStatusCode(1);
                        loginVo1.setMsg("密码已经改正，请登录！");
                        return loginVo1;
                    } else {
                        LoginVo loginVo1 = new LoginVo();
                        loginVo1.setStatusCode(-1);
                        loginVo1.setMsg("ttt666");
                        return loginVo1;
                    }
                }else {
                    LoginVo loginVo1 = new LoginVo();
                    loginVo1.setStatusCode(-1);
                    loginVo1.setMsg("密码改正失败，验证码错误！！");
                    return loginVo1;
                }
            }
        }
        log.info("7777");
        LoginVo loginVo1 = new LoginVo();
        loginVo1.setStatusCode(-1);
        loginVo1.setMsg("密码改正失败，请检查账号和邮箱，或者注册！");
        return loginVo1;
    }
}